<?php

namespace Home\Model;

use Think\Model;

class AccountlogModel extends Model {
    
    protected $tableName = 'account_log'; 
    
    /* 改变用户可用金额,同时做记录 
     * user_id
     * user_name
     * order_id
     * order_sn
     * type
     * states
     * money
     * money_flow
     * complete
     * log_text
     * add_time
     * 
     * edit_field 表示是处理可用金额  还是 冻结金额
     */
    
    function change_money($data, $edit_field = 'money') {
        $member_mod = M('member');
        $member = $member_mod->where('user_id='.$data['user_id'])->find();
        if (empty($member)) {
            render('10106', '用户不存在');
        }
        
        if (empty($data['user_name'])) {
            $data['user_name'] = $member['user_name'];
        }
        if ($data['money_flow'] == 'income') {
            if ($edit_field == 'money_dj') {
                $new_money = array(
                    'money_dj' => $member['money_dj'] + $data['money'],
                );
            } else {
                $new_money = array(
                    'money' => $member['money'] + $data['money'],
                );
            }
        } elseif ($data['money_flow'] == 'outlay') {
            if ($edit_field == 'money_dj') {
                $new_money = array(
                    'money_dj' => $member['money_dj'] - $data['money'],
                );
                if ($new_money['money_dj'] < 0) {
                    render('10118', '冻结金额小于支出金额');
                }
            } else {
                $new_money = array(
                    'money' => $member['money'] - $data['money'],
                );
                if ($new_money['money'] < 0) {
                    render('10119', '可用金额小于支出金额');
                }
            }
        } else {
            render('10120', '资金操作参数错误');
        }
        if($new_money['money']>99999999.99){
            render('10120', '资金操作参数错误');
        }
        $member_mod->where('user_id='.$data['user_id'])->save($new_money); 
        return $this->add_account_log($data);
    }
    function add_account_log($data) {
        if (empty($data['add_time'])) {
            $data['add_time'] = time();
        }
        return $this->add($data);
    }
    
    
    
    
        /**
     * 扣除佣金
     */
    function trade_charges($order_info) {
        //未开启
        if (!C('payment_trade_charges_ratio')) {
            return;
        }
        $payment_trade_charges_ratio = C('payment_trade_charges_ratio');
        $member_mod = M('member');
        
        //当前佣金
        $member_trade_charges = round($order_info['goods_amount'] * $payment_trade_charges_ratio, 2);
        if (empty($member_trade_charges)) {
            return;
        }

        //卖家当前信息
        $seller_info = $member_mod->where('user_id='.$order_info['seller_id'])->find();
        $add_account_log = array(
            'user_id' => $seller_info['user_id'],
            'user_name' => $seller_info['user_name'],
            'order_sn' => $order_info['order_sn'],
            'add_time' => time(),
            'type' => ACCOUNT_TYPE_TRADE_CHARGES,
            'money_flow' => 'outlay', //转出	
            'money' => $member_trade_charges,
            'complete' => 1,
            'log_text' => "扣除交易佣金-你有一笔订单交易成功,扣除佣金" . $member_trade_charges . ",订单号为:" . $order_info['order_sn'] . "商品金额为：" . $order_info['goods_amount'] . ",佣金比例为" . $payment_trade_charges_ratio,
            'states' => ACCOUNT_TYPE_TRADE_CHARGES,
        );
        
        D('member')->change_money($add_account_log);
    }

}

?>
